<template>
  <div class="system-iotDeviceGroup-edit">
    <!-- 添加或修改设备组对话框 -->
    <el-dialog v-model="isShowDialog" width="769px" :close-on-click-modal="false" :destroy-on-close="true">
      <template #header>
        <div >修改
        </div>
      </template>
      <el-form :rules="rules" label-width="100px">
        <el-form-item label="配置组code：">
          <el-input v-model="formData.code" :disabled="true" />
        </el-form-item>
        <el-form-item label="配置组名称：">
          <el-input v-model="apiName" :disabled="true" />
        </el-form-item>
        <el-form-item label="配置项目：">
         <div class="w200">
           <div v-for="item in formData.values" class="aic mb5">
            <div class="w60">{{ item.name }}({{ item.code }})</div>
            <el-input v-model="item.value" :placeholder="item.valueDesc" />
          </div>
         </div>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="onSubmit" :loading="loading">确 定</el-button>
          <el-button @click="onCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { reactive, toRefs, defineComponent, ref, } from 'vue';
import { ElMessage } from 'element-plus';
import { editConfigApi, getConfigInfoApi } from "@/api/thirdManage/apiConfiguration/index";

export default defineComponent({
  name: "apiConfigurationEdit",
  // props: { },
  setup(props, { emit }) {
    const state = reactive({
      loading: false,
      isShowDialog: false,
      formData: {
        code: '',
        values: []
      },
      apiName: '',
      // 表单校验
      rules: {

      }
    });
    // 打开弹窗
    const openDialog = (row) => {
      resetForm();
      if (row) {
        state.apiName = row.api.apiName
          getConfigInfoApi({ id: row.api.apiCode }).then((res: any) => {
            if (res?.data) {
              state.formData.code = res.data.code
              state.formData.values = res.data.items
            }
          })
      }
      state.isShowDialog = true;
    };
    // 关闭弹窗
    const closeDialog = () => {
      state.isShowDialog = false;
    };
    // 取消
    const onCancel = () => {
      closeDialog();
    };
    // 提交
    const onSubmit = () => {
      state.loading = true;
      //修改
      let obj = {
        code: state.formData.code,
        values: {}
      }
      state.formData.values.forEach(item => {
        obj.values[item.code] = item.value
      })
      console.log(obj);
      editConfigApi(obj).then(() => {
        ElMessage.success('修改成功');
        closeDialog(); // 关闭弹窗
        emit('getList')
      }).finally(() => {
        state.loading = false;
      })
    };
    const resetForm = () => {
      state.formData = {
        code: '',
        values: []
      }
      state.apiName = ''
    };
    return {
      openDialog,
      closeDialog,
      onCancel,
      onSubmit,
      ...toRefs(state),
    };
  }
})
</script>
<style scoped></style>